<template>
  <div class="porfile-contiar">
    <van-nav-bar title="个人信息" @click-left="onClickLeft" left-arrow />
    <div class="nav">
      <van-cell is-link title="头像">
        <van-icon name="photo-o" />
      </van-cell>

      <van-cell is-link @click="show2 = true" title="昵称">
        {{ userInfo.name }}
      </van-cell>
      <van-popup v-model="show2" position="bottom" :style="{ height: '100%' , background : '#f5f7f9'}">
        <van-nav-bar
          class="editName"
          title="设置昵称"
          left-text="取消"
          right-text="完成"
          @click-left="onClickLeftName"
          @click-right="onClickRight"
        />
        <van-field
          class="editNameInput"
          v-model="userInfo.name"
          rows="2"
          border
          autosize
          type="textarea"
          maxlength="10"
          show-word-limit
        />
      </van-popup>
      <van-cell is-link @click="show3 = true" title="性别">
        {{ userInfo.gender == 0 ? "男" : "女" }}
      </van-cell>
      <van-popup v-model="show3" position="bottom" :style="{ height: '60%' }">
        <van-picker
          title="标题"
          show-toolbar
          :columns="columns"
          @confirm="onConfirm"
          @cancel="onCancel"
          @change="onChange"
        />
      </van-popup>
      <van-cell is-link @click="show4 = true" title="出生日期">
        {{ userInfo.birthday }}
      </van-cell>
      <van-popup v-model="show4" position="bottom" :style="{ height: '60%' }">
        <van-datetime-picker
          @confirm="confirm"
          v-model="currentDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";

import { editUserInfo, editUser } from "@/api/user";

export default {
  data() {
    return {
      show2: false, // 控制昵称弹出层
      show3: false, // 控制性别弹出层
      show4: false, // 控制出生年月弹出层
      columns: ["男", "女"],
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      userInfo: {},
    };
  },
  methods: {
    // 顶部返回
    onClickLeft() {
      this.$router.go(-1);
    },

    //性别选择
    async onConfirm(value, index) {
      // Toast(`当前值：${value}, 当前索引：${index}`);
      var res = await editUser({ gender: index });
      console.log(res);
      this.$toast.success("修改成功");
      this.getUser();
      this.show3 = false;
    },
    onChange(picker, value, index) {
      Toast(`当前值：${value}, 当前索引：${index}`);
    },
    onCancel() {
      Toast("取消");
    },

    //取消修改昵称
    onClickLeftName() {
      this.show2 = false
    },

    //修改昵称
    async onClickRight() {
      var  res =  await editUser({ name: this.userInfo.name });
      console.log(res);
      this.$toast.success("修改成功");
      this.getUser();
      this.show2 = false;
    },

    // 修改出生日期
    async confirm(value) {
      // console.log(value)

      let filters = this.$root.$options.filters;

      let Timer = filters.datetime(value);

      console.log(Timer);

      var res = await editUser({ birthday: Timer });
      console.log(res);
      this.$toast.success("修改成功");
      this.getUser();
      this.show4 = false;
    },

    //修改前获取用户信息
    async getUser() {
      var { data } = await editUserInfo();

      // console.log(data, 1111);
      this.userInfo = data.data;
      try {
      } catch (err) {
        console.log(err);
      }
    },
  },

  created() {
    if (this.$store.state.userToken) {
      this.getUser();
    }
  },
};
</script>

<style scoped lang='less'>
.porfile-contiar {
 
  .van-nav-bar {
    background-color: hsl(210, 95%, 59%);
    /deep/.van-nav-bar__title {
      color: #fff;
    }
    /deep/.van-icon-arrow-left {
      color: #fff;
    }
  }
  .editName {
    background-color: #fff;
  }
  .editNameInput .van-cell {
    margin: 40px;
  }
  
  
}
</style>